package com.refuel.controller;

import com.refuel.domain.User;
import com.refuel.response.BaseResponse;
import com.refuel.response.Response;
import com.refuel.service.IpService;
import com.refuel.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhanghao
 * @description
 * @date 2024/4/12 08:48
 **/
@RestController
@RequestMapping("/user")
@Tag(name = "用户控制器", description = "用户相关接口")
public class UserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @Resource
    private IpService ipService;

    /**
     * 获取用户ip位置
     * @return Response
     */
    @Operation(summary = "获取用户ip位置接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/ip")
    public BaseResponse<String> ip2Region() {
        String clientIp = MDC.get("clientIp");
        logger.info("clientIp: {}", clientIp);

        String location = ipService.ip2region(clientIp);
        logger.info("location: {}", location);

        return new BaseResponse<>(location);
    }

    /**
     * 保存用户数据
     * @param user 用户信息
     * @return Response
     */
    @Operation(summary = "保存用户数据接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.POST, path = "/login")
    public Response login(@RequestBody User user) {
        logger.info("login user: {}", user);

        userService.save(user);

        return Response.buildResponse();
    }

    /**
     * 查询用户数据
     * @param uid 用户id
     * @return Response
     */
    @Operation(summary = "查询用户数据接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/info")
    public BaseResponse<User> userDetail(@RequestParam(name = "uid") String uid) {
        logger.info("userDetail uid: {}", uid);
        BaseResponse<User> response = new BaseResponse<>();

        User user = userService.findUser(uid);
        response.setData(user);
        return response;
    }
}
